package com.dzsw.netty.basic.nio;

import cn.hutool.core.lang.id.NanoId;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

public class NioFileChannel {

    public static void main(String[] args) throws Exception {
        String src = "D:\\Jenkins.pdf";
        String target = "D:\\11.pdf";
        System.out.println(UUID.randomUUID());
        System.out.println(NanoId.randomNanoId());
        System.out.println(System.currentTimeMillis());
//        read();
//        write();
//        readAndWrite();
//        copyFile(src, target);
    }

    public static void write() throws IOException {
        File file = new File("D:\\test.txt");
        if (!file.exists()) {
            file.createNewFile();
        }
        String str = "这是一次测试。";
        try (RandomAccessFile raf = new RandomAccessFile("D:\\test.txt", "rw")) {
            FileChannel fileChannel = raf.getChannel();
            ByteBuffer buff = ByteBuffer.allocate(1024);
            buff.put(str.getBytes(StandardCharsets.UTF_8));
            buff.flip();
            fileChannel.write(buff);
        }
    }

    public static void read() throws IOException {
        try (RandomAccessFile raf = new RandomAccessFile("D:\\test.txt", "r")) {
            FileChannel fileChannel = raf.getChannel();
            ByteBuffer buff = ByteBuffer.allocate(1024);
            fileChannel.read(buff);
            buff.flip();
            byte[] bytes = new byte[buff.limit()];
            buff.get(bytes, 0, buff.limit());
            String str = new String(bytes);
            System.out.println(str);
        }
    }

    public static void readAndWrite() throws IOException {
        try (RandomAccessFile raf = new RandomAccessFile("D:\\test.txt", "rwd")) {
            FileChannel fileChannel = raf.getChannel();
            ByteBuffer buff = ByteBuffer.allocate(512);
            // read
            fileChannel.read(buff);
            buff.flip();
            byte[] bytes = new byte[buff.limit()];
            buff.get(bytes, 0, buff.limit());
            String str = new String(bytes);
            System.out.println(str);
            // write
            str = "伟大的航路，新的征程！";
            buff.clear();
            buff.put(str.getBytes(StandardCharsets.UTF_8));
            buff.flip();
            fileChannel.write(buff);
            fileChannel.truncate(6);
        }
    }

    public static void copyFile(String src, String target) throws Exception {
        // check源文件是否存在，不存在抛出异常
        File srcFile = new File(src);
        if (!srcFile.exists()) {
            throw new RuntimeException("源文件不存在");
        }
        // check目标文件是否存在，不存在则创建文件
        File targetFile = new File(target);
        if (!targetFile.exists()) {
            targetFile.createNewFile();
        }
//        copyByChannel(srcFile, targetFile);
        copyByTransform(srcFile, targetFile);
    }

    private static void copyByChannel(File srcFile, File targetFile) throws Exception {
        // 打开文件channel
        try (RandomAccessFile srcRaf = new RandomAccessFile(srcFile, "rw");
             RandomAccessFile targetRaf = new RandomAccessFile(targetFile, "rw");
             FileChannel srcChannel = srcRaf.getChannel();
             FileChannel targetChannel = targetRaf.getChannel();) {
            ByteBuffer buff = ByteBuffer.allocate(1024);
            // 读取源文件内容，写入目标文件
            while (true) {
                buff.clear();
                int readLen = srcChannel.read(buff);
                if (readLen == -1) {
                    break;
                }
                buff.flip();
                targetChannel.write(buff);
            }
        }
    }

    private static void copyByTransform(File srcFile, File targetFile) throws Exception {
        // 打开文件channel
        try (RandomAccessFile srcRaf = new RandomAccessFile(srcFile, "rw");
             RandomAccessFile targetRaf = new RandomAccessFile(targetFile, "rw");
             FileChannel srcChannel = srcRaf.getChannel();
             FileChannel targetChannel = targetRaf.getChannel();) {
            // 读取源文件内容，写入目标文件
            targetChannel.transferFrom(srcChannel, 0, srcChannel.size());
        }
    }

    private static final char[] charTable = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b'};

    public static void t() {

    }
}
